<template>
  <div class="home">
    <div class="detail_box">
      <!-- 操作 -->
      <div class="operate_box">
        <div class="item_box">
          <div class="operate_item">
            <label>关键词：</label>
            <el-input type="text" placeholder="请输入关键词" v-model="keyword" clearable></el-input>
          </div>
          <div class="operate_item">
            <span class="operate_btn search_btn" @click="getTableInfo(1)">查询</span>
            <span class="operate_btn reset_btn" @click="resetTable">重置</span>
          </div>
        </div>
        <div class="btn_box" v-if="btn && btn.add">
          <span class="operate_btn search_btn" @click="openPopup('')">新增</span>
        </div>
      </div>
      <!-- 表格 -->
      <div class="table_box">
        <el-table
          border
          v-loading="loading"
          :header-cell-style="tableHeader"
          ref="multipleTable"
          :data="tableInfo"
          tooltip-effect="dark"
          style="width: 100%;">
          <el-table-column
            width="80"
            prop="id"
            label="ID">
          </el-table-column>
          <el-table-column
            width="160"
            sortable
            prop="modifyTime"
            label="修改时间">
            <template slot-scope="scope">
              <p style="text-align: left">{{ formatTime(scope.row.modifyTime) }}</p>
            </template>
          </el-table-column>

          <el-table-column
            prop="title"
            label="标题">
          </el-table-column>

          <el-table-column
            prop="subTitle"
            label="副标题">
          </el-table-column>

          <el-table-column
            label="封面图">
            <template slot-scope="scope">
              <img :src="scope.row.photoUrl" alt="tzs" class="icon" v-if="scope.row.photoUrl" @click="openImg(scope.row.photoUrl)">
              <p v-else>——</p>
            </template>
          </el-table-column>

          <el-table-column
            width="120"
            prop="type"
            label="类型">
            <template slot-scope="scope">
              <div :style="'color:'+(scope.row.noticeType===1?'#67c430':'#6687f9')">
                {{ scope.row.noticeType === 1 ? '单发' : '群发' }}
              </div>
            </template>
          </el-table-column>

          <el-table-column
            prop="time"
            label="发布时间">
            <template slot-scope="scope">
              <p style="text-align: left">{{ scope.row.sendTime }}</p>
            </template>
          </el-table-column>

          <el-table-column
            width="120"
            label="状态">
            <template slot-scope="scope">
              <div :style="{color:sendStatusColor[scope.row.sendStatus]}">
                {{ sendStatus[scope.row.sendStatus]}}
              </div>
            </template>
          </el-table-column>

          <el-table-column
            label="操作"
            fixed="right"
            v-if="btn && (btn.update || btn.delete)"
            width="150">
            <template slot-scope="scope">
              <div class="table_btn">
                <span class="change_info" @click="openPopup(scope.row.id)" v-if="btn.update">编辑</span>
                <span class="change_info delete_btn" @click="handleItem(scope.row.id)" v-if="btn.delete">删除</span>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="record_page">
        <span class="page_count">共<i>{{ totals }}</i>条</span>
        <el-pagination
          @size-change="sizeChange"
          @current-change="getTableInfo"
          background
          :current-page="currentPage"
          :page-sizes="[15, 20, 30, 40]"
          :page-size="pageSize"
          layout="prev, pager, next, sizes,jumper"
          :total="totals">
        </el-pagination>
      </div>
    </div>
    <edit v-if="isPop" :messageId="messageId" @listenToChild="listenToEdit"></edit>
    <ShowImg :icon="icon" @listenToChildEvent="listenToEdit" v-if="isOpen"></ShowImg>
  </div>
</template>

<script>

import edit from "./edit";
import commons from "@/utils/common";
import {noticeList,delData} from "@/api/data/wechat/notice";
import {getBtn} from "@/utils/btnAuth";
import ShowImg from "../../../components/ShowImg";

export default {
  components: {
    edit,
    ShowImg
  },
  data() {
    return {
      keyword: '',
      currentPage: 1,
      pageSize: 15,
      totals: 0,
      loading: false,
      tableInfo: [],
      isPop: false,
      messageId: '',
      firstMenu: '',
      btn: {},
      sendStatus:['待发布','成功','失败'],
      sendStatusColor:['#6687f9','#67c430','#e20000'],
      isOpen:false,
      icon:''
    }
  },
  mounted() {
    this.btn = getBtn();
    if (this.btn.query){
    this.getTableInfo(1);
    }
  },
  methods: {
    // 获取列表信息
    getTableInfo(page) {
      let that = this;
      that.loading = true;
      that.currentPage = page;
      let queryParams = {
        limit: that.pageSize,
        page: page,
      }
      noticeList(queryParams).then(res => {
        that.tableInfo = res ? res.list : [];
        that.totals = res ? res.count : 0;
        that.loading = false;
      }).catch((e) => {
        that.loading = false;
      });
    },
    //删除消息
    handleItem(id){
      let that = this;
      that.$confirm('确定删除消息？','提示',{
        confirmButtonText:'确定',
        cancelButtonText:'取消 ',
        type:"warning"
      }).then(()=> {
        return delData(id);
      }).then(()=> {
        that.$message.success({
          message: '删除成功！',
          duration: 1500
        });
        that.getTableInfo(that.currentPage);
      }).catch(()=> {
      })
      },

    // 重置
    resetTable() {
      this.pageSize = 15;
      this.keyword = '';
      this.getTableInfo(1)
    },
    //查看封面图
    openImg(url) {
      this.isOpen = true;
      this.icon = url;
    },

    // 打开弹窗
    openPopup(id) {
      this.isPop = true;
      this.messageId = id;
    },

    listenToEdit(e) {
      this.isPop = false;
      this.isOpen = false;
      if (e.refresh) {
        this.getTableInfo(this.currentPage)
      }
    },

    formatTime(time) {
      return commons.formatTime(time)
    },

    sizeChange(val) {
      this.pageSize = val;
      this.getTableInfo(1)
    },

    // 设置table标题居中
    tableHeader({row, column, rowIndex, columnIndex}) {
      return 'background:#F0F2F5;color:#111;padding:10px 0;font-size:14px;font-weight:600'
    },
  }
}
</script>

<style scoped>

</style>
